package com.hq.lib_comview.view.imageview;

/**
 * 描述 ：
 * 作者 ：hq
 * 时间 ：2017/8/8 15:14
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.hq.lib_comview.R;

/**
 * Created by Administrator on 2016/9/27 0027.
 */
public class CanvasCircleImageView extends ImageView {
    Paint paint, storkPaint;
    float top;

    public CanvasCircleImageView(Context context) {
        super(context);
        init();
    }

    public CanvasCircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CanvasCircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(3);
        storkPaint = new Paint();
        storkPaint.setColor(getResources().getColor(R.color.color_f5f5f5));
        storkPaint.setAntiAlias(true);                       //设置画笔为无锯齿
        storkPaint.setStrokeWidth((float) 3.0);              //线宽
        storkPaint.setStyle(Paint.Style.STROKE);
        top = getResources().getDimensionPixelSize(R.dimen.px35);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (width > 0) {
            int dis = width < height ? width : height;
            canvas.drawCircle(width / 2, height / 2, dis / 2, paint);
            double angel = Math.acos((dis / 2 - top) / (dis / 2)) / Math.PI * 180;
            RectF rect = new RectF();
            if (width > height) {
                rect.set(width / 2 - height / 2, 0 + 2, width - (width / 2 - height / 2), height - 2);
            } else {
                rect.set(0, (height / 2 - width / 2), width, height - (height / 2 - width / 2));
            }
            canvas.drawArc(rect, (float) (270 - angel), (float) (2 * angel), false, storkPaint);
        }
    }
}
